
document.onkeydown = function (e) { // 回车提交表单
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which;
    if (code == 13) {
        $(".select .select-on").click();
    }
}
layui.use('table', function () {
    var table = layui.table;
    //方法级渲染
    table.render({
        id: 'job'
        , elem: '#jobList'
        , url: '/job/showJobList'
        , cols: [[
            {checkbox: true, fixed: true, width: '5%'}
            , {field: 'jobName', title: '任务名称', width: '10%', sort: true}
            , {field: 'cron', title: '表达式', width: '10%'}
            , {field: 'clazzPath', title: '任务类', width: '20%', sort: true}
            , {field: 'status', title: '状态', width: '10%', sort: true,templet:function (d) {
                    if (d.status === '0'){
                        return '未启用'; }
                    else {
                        return '启用';
                    }
                }}
            , {field: 'jobDesc', title: '任务描述', width: '10%'}
            , {field: 'createDate', title: '创建时间', width: '10%'}
            , {field: 'remark', title: '操作', width: '20%', toolbar: "#toolBar"}
        ]]
        , page: true
        , request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusCode: 200 //成功的状态码，默认：0
            , countName: 'count' //数据总数的字段名称，默认：count
            , dataName: 'data' //数据列表的字段名称，默认：data
        }
        ,even: true
    });

    var $ = layui.$, active = {
        select: function () {
            var jobName = $('#jobName').val();
            var remark = $('#remark').val();
            table.reload('jobList', {
                where: {
                    jobName: jobName,
                    jobDesc: remark
                }
            });
        },
        reload:function(){
            $('#jobName').val('');
            $('#remark').val('');
            table.reload('jobList', {
                where: {
                    jobName: null,
                    jobDesc: null
                }
            });
        },
        add: function () {
            add('添加任务', '/job/showAddJob', 700, 450);
        },
        update: function () {
            var checkStatus = table.checkStatus('jobList')
                , data = checkStatus.data;
            if (data.length != 1) {
                layer.msg('请选择一行编辑', {icon: 5});
                return false;
            }
            if(data[0].status==='1'){
                layer.msg('已经启动任务无法更新,请停止后更新',{icon:5,offset: 'rb',area:['200px','100px'],anim:2});
                return false;
            }
            update('编辑任务', '/job/updateJob?id=' + data[0].id, 700, 450);
        },
        detail: function () {
            var checkStatus = table.checkStatus('jobList')
                , data = checkStatus.data;
            if (data.length != 1) {
                layer.msg('请选择一行查看', {icon: 5});
                return false;
            }
            detail('查看任务信息', '/job/updateJob?id=' + data[0].id, 700, 450);
        }
    };
    //监听工具条
    table.on('tool(job)', function (obj) {
        var data = obj.data;
        if (obj.event === 'detail') {
            detail('编辑角色', '/job/updateJob?id=' + data.id, 700, 450);
        } else if (obj.event === 'del') {
            if(data.status==='0') {
                layer.confirm('确定删除任务[<label style="color: #00AA91;">' + data.jobName + '</label>]?',
                    function () {
                        del(data.id);
                    });
            }else{
                layer.msg('已经启动任务无法更新,请停止后删除',{icon:5,offset: 'rb',area:['200px','100px'],anim:2});
            }
        } else if (obj.event === 'edit') {
            if(data.status==='0'){
                update('编辑任务', '/job/updateJob?id=' + data.id, 700, 450);
            }else{
                layer.msg('已经启动任务无法更新,请停止后更新',{icon:5,offset: 'rb',area:['200px','100px'],anim:2});
            }
        } else if(obj.event === 'start'){
            layer.confirm('确定开启任务[<label style="color: #00AA91;">' + data.jobName + '</label>]?', function(){
                reqByAjax(data.id,'/job/startJob','jobList');
            });
        } else if(obj.event === 'end'){
            layer.confirm('确定停止任务[<label style="color: #00AA91;">' + data.jobName + '</label>]?', function(){
                reqByAjax(data.id,'/job/endJob','jobList');
            });
        }
    });

    $('.layui-col-md12 .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $('.select .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

});


function reqByAjax(id,url,tableId){
    $.ajax({
        url: url,
        type: "post",
        data: {id: id},
        success: function (d) {
            if(d.flag){
                layer.msg(d.msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
                layui.table.reload(tableId);
            }else{
                layer.msg(d.msg,{icon:5,offset: 'rb',area:['120px','80px'],anim:2});
            }
        }
    });
}

function del(id) {
    $.ajax({
        url: "/job/del",
        type: "post",
        data: {id: id},
        success: function (d) {
            if(d.flag){
                layer.msg(d.msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
                layui.table.reload('jobList');
            }else{
                layer.msg(d.msg,{icon:5,offset: 'rb',area:['120px','80px'],anim:2});
            }
        }
    });
}

function detail(title, url, w, h) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "error/404";
    }
    if (w == null || w == '') {
        w = ($(window).width() * 0.9);
    }
    if (h == null || h == '') {
        h = ($(window).height() - 50);
    }
    layer.open({
        id: 'user-detail',
        type: 2,
        area: [w + 'px', h + 'px'],
        fix: false,
        maxmin: true,
        shadeClose: true,
        shade: 0.4,
        title: title,
        content: url + '&detail=true',
        // btn:['关闭']
    });
}
/**
 * 更新用户
 */
function update(title, url, w, h) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "error/404";
    }
    if (w == null || w == '') {
        w = ($(window).width() * 0.9);
    }
    if (h == null || h == '') {
        h = ($(window).height() - 50);
    }
    layer.open({
        id: 'user-update',
        type: 2,
        area: [w + 'px', h + 'px'],
        fix: false,
        maxmin: true,
        shadeClose: false,
        shade: 0.4,
        title: title,
        content: url + '&detail=false'
    });
}

/*弹出层*/
/*
 参数解释：
 title   标题
 url     请求的url
 id      需要操作的数据id
 w       弹出层宽度（缺省调默认值）
 h       弹出层高度（缺省调默认值）
 */
function add(title, url, w, h) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "error/404";
    }
    if (w == null || w == '') {
        w = ($(window).width() * 0.9);
    }
    if (h == null || h == '') {
        h = ($(window).height() - 50);
    }
    layer.open({
        id: 'job-add',
        type: 2,
        area: [w + 'px', h + 'px'],
        fix: false,
        maxmin: true,
        shadeClose: false,
        shade: 0.4,
        title: title,
        content: url
    });
}